package com.loong.web.gp4j.service.message;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.loong.tool.lang.Strings;
import com.loong.web.gp4j.bean.param.messages.MessageParam;
import com.loong.web.gp4j.bean.param.messages.MessagePublishParam;
import com.loong.web.gp4j.bean.param.search.SearchParam;
import com.loong.web.gp4j.bean.schema.message.MessageSchema;
import com.loong.web.gp4j.bean.schema.message.UserMessageSchema;
import com.loong.web.gp4j.bean.schema.user.UserSchema;
import com.loong.web.gp4j.bean.schema.user.UserStatuses;
import com.loong.web.gp4j.bean.vo.message.MessageVo;
import com.loong.web.gp4j.dao.message.MessageDao;
import com.loong.web.gp4j.dao.message.MessageTrans;
import com.loong.web.gp4j.dao.user.UserDao;
import com.loong.web.gp4j.service.user.UserAdapter;
import com.loong.web.suppert.auth.User;
import com.loong.web.suppert.auth.UserSessions;
import com.loong.web.suppert.collection.list.KvnList;
import com.loong.web.suppert.collection.param.PageParam;
import com.loong.web.suppert.collection.param.ParamMap;
import com.loong.web.suppert.collection.vo.ResultVo;
import com.loong.web.suppert.collection.vo.ResultVoFactory;
import com.loong.web.suppert.collection.vo.RowsVo;

/**
 * 消息服务
 *
 * @author 张成轩
 */
@Service
public class MessageService {

	@Resource
	private MessageDao messageDao;
	@Resource
	private MessageTrans messageTrans;
	@Resource
	private UserDao userDao;

	@Resource(name = "order.type")
	private Map<String, String> orderType;
	@Resource(name = "order.field.message")
	private KvnList<String, String, String> orderField;

	@Resource(name = "user.statuses")
	private KvnList<String, Integer, String> statuses;

	/**
	 * @return 排序字段
	 */
	public KvnList<String, String, String> getOrder() {

		return orderField;
	}

	/**
	 * 查询消息列表
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @param page 分页
	 * @return 返回对象
	 */
	public RowsVo<MessageVo> queryMessages(HttpServletRequest request, SearchParam param, PageParam page) {

		ParamMap params = new ParamMap();
		if (Strings.isNotNull(param.getK()))
			params.putLike("k", param.getK());
		// 分页排序
		params.setPage(page);
		params.buildOrder(orderType, orderField);
		// 查询
		List<MessageSchema> messages = messageDao.query(params);
		// 封装对象
		List<MessageVo> vos = new LinkedList<MessageVo>();
		MessageAdapter.convert(messages, vos);
		RowsVo<MessageVo> vo = new RowsVo<MessageVo>();
		vo.setPage(page);
		vo.setRows(vos);
		return vo;
	}

	/**
	 * 删除消息
	 * 
	 * @param id 消息id
	 * @return 返回对象
	 */
	public ResultVo<?> removeMessage(Integer id) {

		int count = messageTrans.delete(id);
		if (count != 1)
			return ResultVoFactory.fail("更新失败");
		return ResultVoFactory.succ();
	}

	/**
	 * 添加消息
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @return 返回对象
	 */
	public ResultVo<?> add(HttpServletRequest request, MessageParam param) {

		MessageSchema message = new MessageSchema();
		MessageAdapter.convert(param, message);

		User user = UserSessions.getUser(request);
		message.setCreater(user.getId());

		int count = messageDao.insert(message);
		if (count != 1)
			return ResultVoFactory.fail("更新失败");
		return ResultVoFactory.succ();
	}

	/**
	 * 发布消息
	 * 
	 * @param param 参数
	 * @return 返回对象
	 */
	public ResultVo<?> publishMessage(MessagePublishParam param) {

		// 查询用户
		List<UserSchema> users = userDao.getByAuths(param.getAuths(), statuses.getV(UserStatuses.NORMAL),
				param.getId());
		if (users.isEmpty())
			return ResultVoFactory.fail("未找到相关用户");
		// 封装对象
		List<UserMessageSchema> messages = new LinkedList<UserMessageSchema>();
		UserAdapter.convert(param, users, statuses, messages);
		// 插入用户消息
		int count = messageTrans.insertUserMessage(messages);
		if (count == 0)
			return ResultVoFactory.fail("更新失败");
		return ResultVoFactory.succ();
	}
}
